using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Net;
using System.Text.RegularExpressions;
using System.Xml;
using System.Web;
using System.Web.Services;
using Dimok.Data.Database;
using Dimok.Exceptions;
using Dimok.User;

////////////////////////////////////////////////////////////////////////////////////////////////////
// namespace: Dimok.GLRules
//
// summary:	.
////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Dimok.GLRules
{
	////////////////////////////////////////////////////////////////////////////////////////////////////
	/// <summary>	Summary description for GLRules1. </summary>
	///
	/// <remarks>	Dimon, 12/21/2010. </remarks>
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public class WS : System.Web.Services.WebService{
		private UserObject uox = new UserObject();

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Default constructor. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public WS()
		{
			//CODEGEN: This call is required by the ASP.NET Web Services Designer
			InitializeComponent();
		}
		
		#region Banks

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets the bank list. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public string Bank_List() {
			try{
				return SQLHelperEx.ADOToString(uox.dbADO_GL,"SELECT * FROM t_Bank");
			}catch(Exception exc){ throw new ClientException(exc); }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Bank delete. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="bankID">	Identifier for the bank. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public void Bank_Delete(int bankID) {
			try{
				SQLHelperEx.ExecuteScalarEx(uox.db_GL,"DELETE FROM t_Bank WHERE ID=@ID",bankID);
			}catch(Exception exc){ throw new ClientException(exc); }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Bank update. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="bankID">			Identifier for the bank. </param>
		/// <param name="fieldName">	Name of the field. </param>
		/// <param name="fieldValue">	The field value. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public void Bank_Update(int bankID,string fieldName,string fieldValue) {
			try{
				SQLHelperEx.UpdateField(uox.db_GL,"SELECT * FROM t_Bank WHERE ID="+bankID,fieldName,fieldValue);
			}catch(Exception exc){ throw new ClientException(exc); }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Bank add. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="dsBank">	Element describing the ds bank. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public int Bank_Add(XmlElement dsBank){
			try{
				DataTable dt = SQLHelperEx.LoadDataTable(dsBank);
				SQLHelperEx.UpdateDataTable(dt,uox.db_GL,"SELECT TOP 0 * FROM t_Bank");
				return (int)SQLHelperEx.ExecuteScalarEx(uox.db_GL,"SELECT ID FROM t_Bank WHERE Name=@Name",dt.Rows[0]["Name"]);
			}catch( Exception exc ){ throw new ClientException(exc); }
		}
		#endregion
		#region Programs

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Program list. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="filter">	The filter. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public string ProgramList(string filter) {
			try{
				if( filter == null || filter == "" )filter = "1=0";
				filter = filter.Replace('*','%');
				return SQLHelperEx.ADOToString(uox.dbADO_GL,"SELECT * FROM t_LoanProgram WHERE "+SQLHelperEx.CheckInjects(filter));
			}catch(Exception exc){ throw new ClientException(exc); }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Program copy. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="programID">		Identifier for the program. </param>
		/// <param name="programName">	Name of the program. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public void Program_Copy(int programID,string programName){
			try{
				SQLHelperEx.ExecuteNonQuery(uox.db_GL,"sp__GL_CopyProgram",programID,programName);
			}catch( Exception exc ){ throw new ClientException(exc); }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Program delete. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="programID">	Identifier for the program. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public void Program_Delete(int programID){
			try{
				SQLHelperEx.ExecuteNonQueryEx(uox.db_GL,"DELETE FROM t_LoanProgram WHERE ID=@ID",programID);
			}catch( Exception exc ){ throw new ClientException(exc); }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Program add. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="dsProgram">	Element describing the ds program. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public int Program_Add(XmlElement dsProgram){
			try{
				DataTable dt = SQLHelperEx.LoadDataTable(dsProgram);
				SQLHelperEx.UpdateDataTable(dt,uox.db_GL,"SELECT TOP 0 * FROM t_LoanProgram");
				return (int)SQLHelperEx.ExecuteScalarEx(uox.db_GL,"SELECT ID FROM t_LoanProgram WHERE BankID=@BankID AND Name=@Name",dt.Rows[0]["BankID"],dt.Rows[0]["Name"]);
			}catch( Exception exc ){ throw new ClientException(exc); }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Program update. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="programID">	Identifier for the program. </param>
		/// <param name="fieldName">	Name of the field. </param>
		/// <param name="fieldValue">	The field value. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public void Program_Update(int programID,string fieldName,string fieldValue){
			try{
				SQLHelperEx.UpdateField(uox.db_GL,"SELECT * FROM t_LoanProgram WHERE ID="+programID,fieldName,fieldValue);
			}catch( Exception exc ){ throw new ClientException(exc); }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets the programs. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="bankID">	Identifier for the bank. </param>
		///
		/// <returns>	The programs. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public string GetPrograms(int bankID){
			try{
				return SQLHelperEx.ExecuteDatasetEx(uox.db_GL,"SELECT ID,Name FROM t_LoanProgram WHERE BankID = @BankID AND ID > 0 ORDER BY Position",bankID).GetXml();
			}catch( Exception exc ){ throw new ClientException(exc); }
		}
		#endregion

		#region PreQual

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets a pq information. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="programID">	Identifier for the program. </param>
		/// <param name="applID">			Identifier for the appl. </param>
		///
		/// <returns>	The pq information. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public string GetPQInfo(int programID,int applID){
			try{
				//string rs = SQLHelperEx.ExecuteDatasetEx(uox.db_GL,"s_PQ_Info",programID,applID).GetXml();
				return SQLHelperEx.AppendRSToString( uox.dbADO_GL,60,"s_PQ_Info",programID,applID);
			}catch( Exception exc ){ throw new ClientException(exc); }
		}
		#endregion PreQual

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Fields list. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="ProgramID">	Identifier for the program. </param>
		/// <param name="raw">				true to raw. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public string FieldsList(int ProgramID,bool raw){return FieldsList(ProgramID,raw,null);}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Fields list. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="ProgramID">	Identifier for the program. </param>
		/// <param name="raw">				true to raw. </param>
		/// <param name="sortColumn">	The sort column. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		public string FieldsList(int ProgramID,bool raw,string sortColumn){
			DataTable dtFD = SQLHelperEx.ExecuteDataset(uox.db_GL,"sp__GL_FieldsDesc",ProgramID).Tables[0];
			if( (sortColumn+"") != "" )SQLHelperEx.SortTable(dtFD,"ColumnName");
			string s = "";
			foreach( DataRow row in dtFD.Rows )
				s += "<OPTION value="+row["ID"]+" type='"+row["TypeName"]+"' "+
					"desc='"+HttpUtility.HtmlEncode(row["Description"]+"")+"' "+
					"lable='"+HttpUtility.HtmlEncode(row["Lable"]+"")+"' "+
					"units='"+HttpUtility.HtmlEncode(row["Units"]+"")+"' "+
					"message='"+HttpUtility.HtmlEncode(row["RejectMessage"]+"")+"' "+
					"enumTable='"+HttpUtility.HtmlEncode(row["EnumTable"]+"")+"' "+
					">"+row[raw?"ColumnName":"Lable"]+"</OPTION>\n";
			return s;
		}

		#region Component Designer generated code
		
		//Required by the Web Services Designer 
		private IContainer components = null;

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	
		/// Required method for Designer support - do not modify the contents of this method with the code
		/// editor. 
		/// </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		private void InitializeComponent()
		{
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Clean up any resources being used. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="disposing">	true if resources should be disposed, false if not. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		protected override void Dispose( bool disposing )
		{
			if(disposing && components != null)
			{
				components.Dispose();
			}
			base.Dispose(disposing);		
		}
		
		#endregion

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Updates the field description described by fieldInfo. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="fieldInfo">	Information describing the field. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public string UpdateFieldDesc(XmlElement fieldInfo){
			try{
				DataSet dsFI = SQLHelperEx.LoadDataSet(fieldInfo);
				string fieldID = dsFI.Tables[0].Rows[0]["ID"].ToString();
				if( fieldID != "0" && fieldID != "" )
					SQLHelperEx.UpdateDataSet(dsFI,uox.db_GL,"SELECT * FROM t_ColumnProperties WHERE ID=@ID",fieldID);
				else{
					SqlCommandBuilder cb = new SqlCommandBuilder(new SqlDataAdapter("SELECT * FROM t_ColumnProperties",uox.db_GL));
					cb.DataAdapter.Update(dsFI.Tables[0]);
					fieldID = SQLHelperEx.ExecuteScalarEx(uox.db_GL,"SELECT ID FROM t_ColumnProperties WHERE FieldName=@FieldName",dsFI.Tables[0].Rows[0]["FieldName"]).ToString();
				}
				return fieldID;
			}catch( Exception exc ){ throw new ClientException(exc); }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Down load. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="url">	URL of the document. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		private byte[] DownLoad(string url){
			WebClient wc = new WebClient();
			byte[] pdf = wc.DownloadData(url);
			pdf = Documents.PDF.Decrypt(pdf);
			return pdf;
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Shows the gl rate sheets. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="programID">	Identifier for the program. </param>
		/// <param name="applID">			Identifier for the appl. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public void ShowGLRateSheets(int programID,int applID){ShowBankGL(programID,GL.GuideLinesType.Rates);}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Shows the gl matrix. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="programID">	Identifier for the program. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public void ShowGLMatrix(int programID){ShowBankGL(programID,GL.GuideLinesType.Matrix);}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Shows the bank gl. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="programID">	Identifier for the program. </param>
		/// <param name="type">				The type. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		private void ShowBankGL(int programID,GL.GuideLinesType type){
			try{
				#region Old stuff
				/*
				string fieldPdf = type + "Pdf";
				string fieldUrl = type + "Url";
				string fieldPages = type + "Pages";
				string fieldText = type + "Text";
				Context.Response.Cache.SetMaxAge(new TimeSpan(0,0,30));
				DataRow drLPs = SQLHelperEx.ExecuteDatasetEx(uox.db_GL,
					"SELECT "+fieldPdf+",ISNULL("+fieldUrl+",'') "+fieldUrl+",ISNULL("+fieldPages+",'') "+fieldPages+",ISNULL("+fieldText+",'') "+fieldText+" FROM t_LoanProgram WHERE ID = @ID",programID).Tables[0].Rows[0];
				byte[] pdf = drLPs[fieldPdf] == DBNull.Value ? new byte[]{} : (byte[])drLPs[fieldPdf];
				string url = drLPs[fieldUrl]+"";
				byte[] pdfNew = DownLoad(url);
				if( drLPs[fieldPages].ToString() != "" )
					pdfNew = AcrobatObjects.AcrobatClient.GetPages(pdfNew,drLPs[fieldPages]+"");
				if( pdf.Length == 0 && pdfNew.Length == 0 ){
					Context.Response.Write("No matrix file found.");
					return;
				}
				if( pdfNew.Length > 0 && pdfNew.Length != pdf.Length || drLPs[fieldText].ToString() == "" ){
					string text = string.Join(Environment.NewLine,AcrobatObjects.AcrobatClient.GetText(pdfNew));
					SQLHelperEx.UpdateDataTable(new HashTableEx(fieldPdf,pdfNew,fieldText,text),uox.db_GL,"SELECT ID,"+fieldPdf+","+fieldText+" FROM t_LoanProgram WHERE ID = @ID",programID);
					pdf = pdfNew;
				}
				*/
				#endregion
				Hashtable ht = GL.GetProgramGLs(uox.db_GL,programID,type);
				byte[] pdf = (byte[])ht["pdf"];
				if( pdf.Length > 0 ){
					Context.Response.ContentType = "application/pdf";
					Context.Response.BinaryWrite(pdf);
				}else
					Context.Response.Write("No guidelines for this program.");
			}catch( Exception exc ){
				ClientException.Publish(exc,new NameValueCollectionEx("ProgID",programID+"","Type",type+""));
				Context.Response.Write(ClientException.GetMessages(exc));
			}
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Shows the gl text. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <param name="programID">	Identifier for the program. </param>
		/// <param name="type">				The type. </param>
		/// <param name="suffix">			The suffix. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public void ShowGLText(int programID,string type,string suffix){
			if( !Regex.IsMatch(type,"^(matrix|rates)$",RegexOptions.IgnoreCase) )return;
			if( !Regex.IsMatch(suffix,"^(|old)$",RegexOptions.IgnoreCase) )return;
			string fieldText = type + "Text"+suffix;
			DataRow drLPs = SQLHelperEx.ExecuteDatasetEx(uox.db_GL,"SELECT ISNULL("+fieldText+",'') "+fieldText+" FROM t_LoanProgram WHERE ID = @ID",programID).Tables[0].Rows[0];
			string text = drLPs[fieldText]+"";
			Context.Response.Cache.SetMaxAge(new TimeSpan(0,0,10));
			Context.Response.ContentType = "text/html";
			Context.Response.AddHeader("content-disposition","attachment; filename=\""+type+"_"+suffix+".txt\"");
			Context.Response.Write(text);
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Gets a gl tree. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="programID">	Identifier for the program. </param>
		/// <param name="applID">			Identifier for the appl. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public void GetGLTree(int programID,int applID){
			try{
				string tree = GL.GetGLRulesXML(uox.db_GL,programID,applID>0?applID:(uox.hasApplID?uox.applID:0)).Replace("NewDataSet","TREENODES").Replace("MYTREE:","");
				Context.Response.Write(tree);
			}catch( Exception exc ){ throw new ClientException(exc); }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Builds a gl rules. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="programID">	Identifier for the program. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public void BuildGLRules(int programID){
			try{
				SQLHelperEx.ExecuteNonQueryEx(uox.db_GL,"sp__GL_MakeDecide_Program",programID,null);
			}catch( Exception exc ){ throw new ClientException(exc); }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Removes the gl condition described by ruleID. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="ruleID">	Identifier for the rule. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public void RemoveGLCondition(int ruleID){
			try{
				SQLHelperEx.ExecuteNonQueryEx(uox.db_GL,"DELETE FROM t_GLRules WHERE ID=@ID",ruleID);
			}catch( Exception exc ){ throw new ClientException(exc); }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Inserts a gl condition. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="programID">	Identifier for the program. </param>
		/// <param name="parentID">		Identifier for the parent. </param>
		/// <param name="prevID">			Identifier for the previous. </param>
		/// <param name="columnID">		Identifier for the column. </param>
		/// <param name="condition">	The condition. </param>
		/// <param name="value0">			The value 0. </param>
		/// <param name="value1">			The first value. </param>
		/// <param name="label">			The label. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public int InsertGLCondition(string programID,string parentID,string prevID,int columnID,string condition,string value0,string value1,string label){
			try{
				int ruleID = (int)SQLHelperEx.ExecSP(uox.db_GL,"sp__GL_InsertConditionItem",parentID,prevID,columnID,condition,value0,value1,label,programID);
				return ruleID;
			}catch( Exception exc ){ throw new ClientException(exc); }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Combine gl condition. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="prevID">			Identifier for the previous. </param>
		/// <param name="columnID">		Identifier for the column. </param>
		/// <param name="condition">	The condition. </param>
		/// <param name="value0">			The value 0. </param>
		/// <param name="value1">			The first value. </param>
		/// <param name="label">			The label. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public void CombineGLCondition(int prevID,int columnID,string condition,string value0,string value1,string label){
			try{
				SQLHelperEx.ExecuteNonQuery(uox.db_GL,"sp__GL_CombineConditionItem",prevID,columnID,condition,value0,value1,label);
			}catch( Exception exc ){ throw new ClientException(exc); }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Copies the gl condition. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="prevID">		Identifier for the previous. </param>
		/// <param name="afterID">	Identifier for the after. </param>
		/// <param name="parentID">	Identifier for the parent. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public int CopyGLCondition(int prevID,string afterID,string parentID){
			try{
				return (int)SQLHelperEx.ExecSP(uox.db_GL,"sp__GL_CopyItem",prevID,parentID,afterID);
			}catch( Exception exc ){ throw new ClientException(exc); }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Move gl condition. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="itemID">		Identifier for the item. </param>
		/// <param name="afterID">	Identifier for the after. </param>
		/// <param name="parentID">	Identifier for the parent. </param>
		/// <param name="copy">			true to copy. </param>
		///
		/// <returns>	. </returns>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public int MoveGLCondition(int itemID,string afterID,string parentID,bool copy){
			try{
				if( copy )return CopyGLCondition(itemID,afterID,parentID);
        else return (int)SQLHelperEx.ExecSP(uox.db_GL,"sp__GL_MoveItem",itemID,afterID,parentID);
			}catch( Exception exc ){ throw new ClientException(exc); }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Updates the gl condition. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="ID">					The identifier. </param>
		/// <param name="columnID">		Identifier for the column. </param>
		/// <param name="condition">	The condition. </param>
		/// <param name="value0">			The value 0. </param>
		/// <param name="value1">			The first value. </param>
		/// <param name="label">			The label. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public void UpdateGLCondition(int ID,int columnID,string condition,string value0,string value1,string label){
			try{
				SQLHelperEx.ExecuteDatasetEx(uox.db_GL,"UPDATE t_GLRules SET ColumnID=@ColumnID,Condition=@Condition,Value=@Value,Value1=@Value1,Label=@Label WHERE ID=@ID",columnID,condition,value0,value1,label,ID);
			}catch( Exception exc ){ throw new ClientException(exc); }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Updates the gl value. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="ID">						The identifier. </param>
		/// <param name="valueNumber">	The value number. </param>
		/// <param name="value_">				The value. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public void UpdateGLValue(int ID,int valueNumber,string value_){
			try{
				string valueName = "Value"+(valueNumber==1?"1":"");
				SQLHelperEx.ExecuteDatasetEx(uox.db_GL,"UPDATE t_GLRules SET "+valueName+"=@Value WHERE ID=@ID",value_,ID);
			}catch( Exception exc ){ throw new ClientException(exc); }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Updates the gl label. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="ID">			The identifier. </param>
		/// <param name="label">	The label. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public void UpdateGLLabel(int ID,string label){
			try{
				SQLHelperEx.ExecuteDatasetEx(uox.db_GL,"UPDATE t_GLRules SET Label=@Label WHERE ID=@ID",label==""?null:(object)label,ID);
			}catch( Exception exc ){ throw new ClientException(exc); }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Change gl label. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="ID">			The identifier. </param>
		/// <param name="label">	The label. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public void ChangeGLLabel(int ID,string label){
			try{
				SQLHelperEx.UpdateField(uox.db_GL,"SELECT * FROM t_GLRules WHERE ID="+ID,"Label",label);
			}catch( Exception exc ){ throw new ClientException(exc); }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Shift gl condition. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="ID">		The identifier. </param>
		/// <param name="step">	Amount to increment by. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public void ShiftGLCondition(int ID,int step){
			try{
				SQLHelperEx.ExecuteNonQueryEx(uox.db_GL,"UPDATE t_GLRules SET Position=dbo.MaxNum(1,Position+@Step) WHERE ID=@ID",step,ID);
			}catch( Exception exc ){ throw new ClientException(exc); }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Updates the real time described by realTime. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="realTime">	Time of the real. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public void UpdateRealTime(bool realTime){
			try{
				SQLHelperEx.ExecuteNonQueryEx(uox.db_GL,"sp__sconf_RealTime",realTime);
			}catch( Exception exc ){ throw new ClientException(exc); }
		}

		////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>	Updates the tree debug described by treeDebug. </summary>
		///
		/// <remarks>	Dimon, 12/21/2010. </remarks>
		///
		/// <exception cref="ClientException">	Thrown when client. </exception>
		///
		/// <param name="treeDebug">	true to tree debug. </param>
		////////////////////////////////////////////////////////////////////////////////////////////////////

		[WebMethod]
		public void UpdateTreeDebug(bool treeDebug){
			try{
				SQLHelperEx.ExecuteNonQueryEx(uox.db_GL,"sp__sconf_TreeDebug",treeDebug);
			}catch( Exception exc ){ throw new ClientException(exc); }
		}
	}
}
